package valuecomusa.fijibus.eticket;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import valuecomusa.fijibus.eticket.shorthaul.R;
import valuecomusa.fijibus.eticket.shorthaul.Main;
import valuecomusa.fijibus.eticket.shorthaul.SunBeamStartUp;

import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class SunbeamActivity extends Activity
{

    Handler handler;
    public Button btnHome;
    public Button btnSignOut;
    public Button btnBack;

    public void doCommon()
    {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectNetwork() // or .detectAll() for all detectable problems
                .penaltyLog().build());

		UpdateBottomLine();

        TextView title = (TextView)findViewById(R.id.title_text);
        if(title != null)
            title.setTextColor(Color.rgb(238, 238, 0));

        btnHome = (Button) findViewById(R.id.btHome);
        if (btnHome != null)
        {
            btnHome.setOnClickListener(new View.OnClickListener()
            {

                public void onClick(View v)
                {
                    Intent homeInt = new Intent(getApplicationContext(),
                            Main.class);
                    homeInt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(homeInt);
                    finish();
                }
            });
        }

        btnSignOut = (Button) findViewById(R.id.btSignOut);
        if (btnSignOut != null)
        {
            btnSignOut.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {

                    //---
					if(Main.getInstance() != null)
					{
                        String x = WorkingSession.getInstance().currentActivity.getClass().toString();
                        String y = Main.class.toString();
                        //--
                        Intent mainIntent = new Intent(getApplicationContext(),
                                Main.class);
                        mainIntent.putExtra("LOG_OUT", 1);
                        Main.getInstance().setIntent(mainIntent);
                        //--
                        if(x.equals(y))
                        {
                            mainIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                            startActivity(mainIntent);
                            return;
                        }
					}
					else
					{
                        WorkingSession.getInstance().Reset();
                        Intent loginIntent = new Intent(getApplicationContext(),
                                SunBeamStartUp.class);
                        //loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(loginIntent);

					}
                    finish();
                }
            });
        }

        btnBack = (Button) findViewById(R.id.btBack);
        if (btnBack != null)
        {
            btnBack.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    onBackPressed();
                }
            });
        }
    }

	private TextView title_app_version;
	public void UpdateBottomLine()
	{
		Resources res = getResources();
		if(title_app_version != null)
		{
			title_app_version.setText(
					res.getString(R.string.app_name) + " " + res.getString(R.string.version) +
							" --- TX count: " + WorkingSession.getInstance().transactionCount
			);
			return;
		}
		LinearLayout rootView = (LinearLayout)findViewById(R.id.root_view);
		if(rootView != null)
		{
			RelativeLayout layout = new RelativeLayout(this);
			LayoutParams param = new LayoutParams(
					LayoutParams.WRAP_CONTENT,  //width
					LayoutParams.WRAP_CONTENT); //height
			layout.setLayoutParams(param);

			title_app_version = new TextView(this);
			param = new LayoutParams(
					LayoutParams.WRAP_CONTENT,  //width
					LayoutParams.WRAP_CONTENT); //height

			title_app_version.setLayoutParams(param);
			title_app_version.setTextColor(Color.rgb(0, 0, 0));
			//---
			title_app_version.setText(
					res.getString(R.string.app_name) + " " + res.getString(R.string.version) +
							" --- TX count: " + WorkingSession.getInstance().transactionCount
			);
			layout.addView(title_app_version);

			rootView.addView(layout);
		}
	}

	@Override
	protected void onResume()
	{
        handler =  new Handler() {
            @Override
            public void close() {

            }

            @Override
            public void flush() {

            }

            @Override
            public void publish(LogRecord record) {

            }
        };
		super.onResume();
		Log.d("SunbeamActivity", "onResume: " + this.getClass().toString());
		WorkingSession.getInstance().currentActivity = this;
	}
}
